package io.renren.common.utils;


import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.modules.work.dao.ScGoodsDao;
import io.renren.modules.work.dao.ScPurDao;
import io.renren.modules.work.dao.ScPurdtlDao;
import io.renren.modules.work.entity.ExcelData_Pur;
import io.renren.modules.work.entity.ScGoodsEntity;
import io.renren.modules.work.entity.ScPurEntity;
import io.renren.modules.work.entity.ScPurdtlEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 给Listener创建成员变量，然后在构造方法里面传进去。必须不让spring管理Listener，每次读取    都要new一个。
 */
public class ExcelListener_Pur extends AnalysisEventListener<ExcelData_Pur> {


    private ScPurDao scPurDao;

    private ScPurdtlDao scPurdtlDao;

    private ScGoodsDao  scGoodsDao;

    private static final Logger logger = LoggerFactory.getLogger(ExcelListener.class);
    private static final int BATCH_COUNT = 500;
    String id = ConfigConstant.getId();
    List<ScPurdtlEntity> scPurdtlList = new ArrayList<>(1000);

    public ExcelListener_Pur(ScPurDao baseMapper, ScPurdtlDao scPurdtlDao, ScGoodsDao scGoodsDao) {
        this.scPurDao = baseMapper;
        this.scPurdtlDao = scPurdtlDao;
        this.scGoodsDao = scGoodsDao;
    }




    @Override
    public void invoke(ExcelData_Pur excelData_pur, AnalysisContext analysisContext) {

        QueryWrapper<ScGoodsEntity> scGoodsEntityQueryWrapper = new QueryWrapper<>();
        scGoodsEntityQueryWrapper.eq("goodsname",excelData_pur.getGoodsname());
        scGoodsEntityQueryWrapper.eq("goodstype",excelData_pur.getGoodstype());
        List<ScGoodsEntity> scGoodsEntities = scGoodsDao.selectList(scGoodsEntityQueryWrapper);
        Long goodsid = 0L;
        if(scGoodsEntities.size()>0){
            ScGoodsEntity goodsEntity = scGoodsEntities.get(0);
            goodsid = goodsEntity.getGoodsid();
        }else{
            ScGoodsEntity scGoodsEntity = new ScGoodsEntity();
            scGoodsEntity.setGoodsno(ConfigConstant.generateGoodsNo());
            scGoodsEntity.setGoodsname(excelData_pur.getGoodsname());
            scGoodsEntity.setGoodsunit(excelData_pur.getGoodsunit());
            scGoodsEntity.setGoodstype(excelData_pur.getGoodstype());
            scGoodsEntity.setGoodsstate("1");
            goodsid = scGoodsDao.insertGetKey(scGoodsEntity);
        }


        ScPurdtlEntity purdtl = new ScPurdtlEntity();
        purdtl.setPurdtlno(ConfigConstant.generatePurdtlNo());
        purdtl.setPurdtlStatus(ConfigConstant.PURSTATUS_INITIAL);
        purdtl.setGoodsname(excelData_pur.getGoodsname());
        purdtl.setGoodsid(goodsid+"");
        purdtl.setGoodsunit(excelData_pur.getGoodsunit());
        purdtl.setSingleprice(excelData_pur.getSingleprice());
        purdtl.setGoodsnum(excelData_pur.getGoodsnum());
        purdtl.setPrice(excelData_pur.getPrice());
        purdtl.setPurid(id);
        scPurdtlList.add(purdtl);
        if(scPurdtlList.size()>=BATCH_COUNT){
            saveData();
        }

    }

    /**
     * 所有数据解析完成了会调用
     */
    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        //将列表中剩余的数据也插入到数据库中
        if(!scPurdtlList.isEmpty() ) {
            saveData();
        }
        logger.info("所有数据存储完成");
    }
    //数据库持久化操作
    void saveData() {
        ScPurEntity pur = new ScPurEntity();
        pur.setPurid(id);
        pur.setPurname("采购单导入"+id);
        pur.setCretime(new Date());
        pur.setPurtype(ConfigConstant.PURTYPE_NORMAL);
        pur.setPurno(ConfigConstant.generatePurNo());
        pur.setPurstatus(ConfigConstant.PURSTATUS_INITIAL);
        scPurDao.insert(pur);
        scPurdtlDao.batchInsert(scPurdtlList);
        scPurdtlList.clear();
    }
}